package cn.mcch.investigator.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ExpandableListView;

import cn.mcch.investigator.customview.pulltorefresh.PullToRefreshLayout;
import cn.mcch.investigator.listener.OnCommonClickListener;
import cn.mcch.investigator.model.BaseModel;
import cn.mcch.investigator.presenter.BasePresenter;
import cn.mcch.investigator.viewdelegate.BaseViewDelegate;

/**
 * @param <T> BaseViewDelegate
 * @param <M> Model，界面有请求网络的操作时使用
 * @param <P> Presenter
 * @author Cao Mingming
 * @date 2016-5-4 下午5:03:19
 */
public abstract class BaseActivity<T extends BaseViewDelegate, M extends BaseModel, P extends BasePresenter<T, M>>
        extends AppCompatActivity implements OnCommonClickListener {

    public P mPresenter;
    /**
     * 是否开启沉浸式状态栏标记
     */
    public boolean isTranslucentStatus = true;

    /**
     * 构造方法
     */
    public BaseActivity() {
        try {
            // 初始化Presenter
            mPresenter = getPresenterClass().newInstance();
        } catch (Exception e) {
            throw new RuntimeException("create Presenter error!");
        }
    }

    public abstract Class<P> getPresenterClass();

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        // 解决Android应用从市场安装完成打开与桌面打开，被启动两次的问题
        if (!this.isTaskRoot()) {
            Intent intent = this.getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    this.finish();
                    return;
                }
            }

            return;
        }
        // 设置沉浸式状态栏，只对api19以上版本有效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(isTranslucentStatus);
        }
        // 去掉app默认title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 创建根布局
        mPresenter.createRootView(getApplicationContext());
        // 初始化actionbar
        initTitleBar();
        // 初始化content
        mPresenter.initConetentView(getApplicationContext());
        // 创建界面要展示的第一个布局
        createFirstView();
        setContentView(mPresenter.getRootView());
        // 获取上一个activity通过intent传递过来的数据
        getIntentData();
        // 绑定数据
        initBindData();
        // 请求数据
        initData();

    }

    /**
     * 需要title bar重写该方法 调用presenter.initActionbar
     */
    public void initTitleBar() {

    }

    /**
     * 重写该方法 调用presenter.createFirstView
     */
    public abstract void createFirstView();

    /**
     * 获取上一个activity传入的intent数据，需要时重写
     */
    public void getIntentData() {

    }

    /**
     * 界面初始化需要加载数据时重写，调用presenter的initData
     */
    public void initData() {

    }

    /**
     * 初始化绑定数据，需要时重写
     */
    public void initBindData() {

    }

    /**
     * 点击监听，需要时重写
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == getResources().getIdentifier("bt_load_again", "id", getPackageName())) {
            // 重新加载数据
            createFirstView();
            initData();
        } else if (v.getId() == getResources().getIdentifier("rl_titlebar_left", "id", getPackageName())) {
            this.finish();
        }
    }

    /**
     * 长按点击监听，需要时重写
     */
    @Override
    public boolean onLongClick(View v) {
        return false;
    }

    /**
     * 条目点击监听，需要时重写
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        return false;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 条目长按点击监听，需要时重写
     */
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.clear();
            mPresenter = null;
        }
    }

    /**
     * 设置Activity是否全屏
     *
     * @param on 设置是否全屏开关
     * @return true 全屏<br>
     * false 不全屏
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {

    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

    }
}
